﻿#ifndef WIDGETPOINTVIEW_H
#define WIDGETPOINTVIEW_H

#include "Common/common.h"
#include "Parser/customfileparser.h"

#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLFunctions_4_5_Core>
#include <QOpenGLShaderProgram>

class WidgetPointView : public QOpenGLWidget, public QOpenGLFunctions_4_5_Core
{
    Q_OBJECT
public:
    explicit WidgetPointView(QWidget *parent = nullptr);

    void focusProp();

    void deleteSelectPoint();

    void restorePreviousDelete();

    void exportPoint();

    void clear();

    inline void showLine(bool status) { mShowLineStatues = status; update(); };

    inline void showGrid(bool status) { mShowGridStatues = status; update(); };

    inline void showTiff(bool status) { mShowTiffStatues = status; update(); };

    inline void setPointMode(uint8_t mode) { mPointMode = mode % 3;  focusProp(); };

private:
    void init();

protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

    void wheelEvent(QWheelEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;

    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;

private:
    void drawGrid();

    void drawTiff();

    void drawLabels();

    void drawPoint();

    void drawHighlightPoint();

    void drawLine();

    void drawBox();

    QPointF mapToOpenGL(const QPointF &point);

    QRectF boundingRect();

    QImage createTextImgae(double xPos, double xLineSize, double yPos, double yLineSize, double space, QColor fontColor);

    void highlightPoint();

private slots:
    void slot_parse_file_result(const QString &path, const QList<PointDetail> &result);

    void slot_parse_tiff_result(const TiffDetail& result);

private:
    // 着色器
    QOpenGLShaderProgram mShaderProgram;

    // 着色器状态
    bool mShaderProgramLinked = false;

    // 纹理对象
    unsigned int mTextureID = -1;

    // 纹理长度
    int mTextureWidth = 0;

    // 纹理高度
    int mTextureHeight = 0;

    // 点云数据
    QList<PointDetail> mPointList;

    // 图像数据
    TiffDetail mTiffDetail = {false, 0, 0, 0, 0, QImage()};

    // 原点信息
    PointDetail mOriginPointDerail = {"", 0, 0, 0, 0};

    // 缩放比例
    double mViewZoom = 1;

    // 缩放基础
    double mBaseZoomLength = 1.0;

    // 位移
    double mXOffset = 0;
    double mYOffset = 0;

    QPointF mLastMousePosition;
    bool mMousePressed = false;

    QPointF mCurrentMousePosition;

    // 选择标志
    bool mSelectMode = false;

    // 选择范围
    QRectF mRectSelect;

    // VAO
    unsigned int mVAOArray[7];

    // VBO
    unsigned int mVBOArray[7];

    // EBO
    unsigned int mEBOArray[3];

    // 文件解析工具类
    CustomFileParser mCustomFileParser;

    // 显示连线 标志
    bool mShowLineStatues = false;

    // 显示网格 标志
    bool mShowGridStatues = true;

    // 显示 底图 标志
    bool mShowTiffStatues = true;

    // 点云模式
    int mPointMode = 0;
};

#endif // WIDGETPOINTVIEW_H
